package com.hbyj.struts.action;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.hbyj.db.function.DbTools;
import com.hbyj.hbm.vo.CgybtjhzTable;
import com.hbyj.hbm.vo.ZqtjhzTable;
import com.hbyj.util.Constants;
import com.hbyj.util.PageClass;
import com.opensymphony.xwork2.ActionSupport;

public class BbscAction extends ActionSupport{
	private String jsonResult;
	private int total;
	private List rows = new ArrayList();
	
	public String getJsonResult() {
		return jsonResult;
	}

	public void setJsonResult(String jsonResult) {
		this.jsonResult = jsonResult;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public List getRows() {
		return rows;
	}

	public void setRows(List rows) {
		this.rows = rows;
	}
	
	public String queryZQBG(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String pageNo = request.getParameter("page");
		int pageSize = Constants.PAGE_KEY;
		String zhCode = request.getParameter("zhCode");
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		String hql = "from YjdcYjdc01 where yjdc01010<= '"+endTime+"' and yjdc01010>= '"+startTime+"' and yjdc01000 like '"+zhCode+"'";
		String countHql = "select count(*) from YJDC_YJDC01 where YJDC01010<= '"+endTime+"' and YJDC01010>= '"+startTime+"' and YJDC01000 like '"+zhCode+"'";
		int t = DbTools.getDataSQLCount(countHql);
		PageClass page = new PageClass();
		page.setPageShow(pageSize);  //设置每页显示的行数
		page.setPageCount(t);  //设置总页数
		page.setCurrentPage(Integer.parseInt(pageNo));  //设置当前页
		int first=page.getNextIndex(); //获得开始的索引值
		int max=page.getPageShow();  //获得每次显示的值
		rows = (List)DbTools.findPageHQL(first, max,hql);
		total = t;
		return SUCCESS;
	}
	
	
	public String queryZQTJHZ(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String pageNo = request.getParameter("page");
		int pageSize = Constants.PAGE_KEY;
		String zhCode = request.getParameter("zhCode");
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		List listDate = splitDateByStartAndEnd(startTime,endTime);
		List resultList = queryZQTJHZBylistDate(listDate,zhCode);
		//通过切割的日期list去遍历查询，得到各个需要的数量
		int t = listDate.size();
		PageClass page = new PageClass();
		page.setPageShow(pageSize);  //设置每页显示的行数
		page.setPageCount(t);  //设置总页数
		page.setCurrentPage(Integer.parseInt(pageNo));  //设置当前页
		int first=page.getNextIndex(); //获得开始的索引值
		int max=page.getPageShow();  //获得每次显示的值
		rows = resultList;
		total = t;
		return SUCCESS;
	}
	
	
	//根据传递过来的年月日，切成查询的数组
	public List splitDateByStartAndEnd(String startTime,String endTime){
		List list = new ArrayList();
		String startDate = startTime.substring(0, 11);
		String endDate = endTime.substring(0, 11);
		String[] startDateArray = startDate.split("-");
		String[] endDateArray = endDate.split("-");
		String startMonth = startDateArray[1];
		String endMonth = endDateArray[1];
		int startMonthInt = Integer.parseInt(startMonth);
		int endMonthInt = Integer.parseInt(endMonth);
		int yearInt = Integer.parseInt(startDateArray[0]);
		for(int i = startMonthInt;i<=endMonthInt;i++){
			List<String> startToEnd = new ArrayList<String>();
			int biggerDay = getBiggerDayByYearMonth(yearInt, i);
			if(i==startMonthInt){
				startToEnd.add( ""+yearInt+"-"+"0"+i+"-"+startDateArray[2]);
				startToEnd.add(""+yearInt+"-"+"0"+i+"-"+biggerDay);
			}else if(i==endMonthInt){
				if(i<10){
					startToEnd.add( ""+yearInt+"-"+"0"+i+"-"+"01");
					startToEnd.add(""+yearInt+"-"+"0"+i+"-"+endDateArray[2]);
				}else{
					startToEnd.add( ""+yearInt+"-"+i+"-"+"01");
					startToEnd.add( ""+yearInt+"-"+i+"-"+endDateArray[2]);
				}
			}else{
				if(i<10){
					startToEnd.add( ""+yearInt+"-"+"0"+i+"-"+"01");
					startToEnd.add(""+yearInt+"-"+"0"+i+"-"+biggerDay);
				}else{
					startToEnd.add( ""+yearInt+"-"+i+"-"+"01");
					startToEnd.add( ""+yearInt+"-"+i+"-"+biggerDay);
				}
			}
			list.add(startToEnd);
		}
		return list;
	}
		
	//拿到此年份月份下的最大天数
	public int getBiggerDayByYearMonth(int year,int month){
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, month-1);
		return  c.getActualMaximum(Calendar.DAY_OF_MONTH);
	}
	
	//
	public List queryZQTJHZBylistDate(List listDate,String zhCode){
		List<ZqtjhzTable> resultList = new ArrayList<ZqtjhzTable>();
		for(int i = 0;i<listDate.size();i++){
			List<String> list = (List)listDate.get(i);
			String startDate = list.get(0);
			String endDate = list.get(1);
			int yue = Integer.parseInt(startDate.split("-")[1]);
			String zqzsSql = "select count(*) from YJDC_YJDC01 where YJDC01010 >= '"+startDate+"' and YJDC01010<='"+endDate+"' and YJDC01000 like '"+zhCode+"'";
			String yfys_zrSql = zqzsSql +" and YJDC01140='5'";
			String yfys_rwSql = zqzsSql +" and YJDC01140<'5'";
			String zhjb_tdxSql = zqzsSql +" and YJDC01240='1'";
			String zhjb_dxSql = zqzsSql +" and YJDC01240='2'";
			String zhjb_zxSql = zqzsSql +" and YJDC01240='3'";
			String zhjb_xxdbSql = zqzsSql +" and ((YJDC01180>0 and YJDC01180<3) or (YJDC01170>1 and YJDC01170<100))";//达标
			String zhjb_xxwdbSql = zqzsSql +" and (YJDC01180=0 or YJDC01170<1)";//未达标
			String zhlx_hpSql = zqzsSql +" and YJDC01020='01'";
			String zhlx_btSql = zqzsSql +" and YJDC01020='02'";
			String zhlx_nslSql = zqzsSql +" and YJDC01020='03'";
			String zhlx_txSql = zqzsSql +" and YJDC01020='04' or YJDC01020='05'";
			String zhlx_dlfSql = zqzsSql +" and YJDC01020='06'";//地裂缝，没有？现在是斜坡
			String zhlx_dmcjSql = zqzsSql +" and YJDC01020='07'";
			String swrs_swSql = "select sum(YJDC01180) from YJDC_YJDC01 where YJDC01010 >= '"+startDate+"' and YJDC01010<='"+endDate+"' and YJDC01000 like '"+zhCode+"'";
			String swrs_szSql ="select sum(YJDC01181) from YJDC_YJDC01 where YJDC01010 >= '"+startDate+"' and YJDC01010<='"+endDate+"' and YJDC01000 like '"+zhCode+"'";
			String swrs_ssSql = "select sum(YJDC01182) from YJDC_YJDC01 where YJDC01010 >= '"+startDate+"' and YJDC01010<='"+endDate+"' and YJDC01000 like '"+zhCode+"'";
			String jjssSql  = "select sum(YJDC01170) from YJDC_YJDC01 where YJDC01010 >= '"+startDate+"' and YJDC01010<='"+endDate+"' and YJDC01000 like '"+zhCode+"'";
			int zqzs = DbTools.getDataSQLCount(zqzsSql);
			int yfys_zr = DbTools.getDataSQLCount(yfys_zrSql);
			int yfys_rw = DbTools.getDataSQLCount(yfys_rwSql);
			int zhjb_tdx = DbTools.getDataSQLCount(zhjb_tdxSql);
			int zhjb_dx = DbTools.getDataSQLCount(zhjb_dxSql);
			int zhjb_zx = DbTools.getDataSQLCount(zhjb_zxSql);
			int zhjb_xxdb = DbTools.getDataSQLCount(zhjb_xxdbSql);
			int zhjb_xxwdb =DbTools.getDataSQLCount(zhjb_xxwdbSql);
			int zhlx_hp = DbTools.getDataSQLCount(zhlx_hpSql);
			int zhlx_bt = DbTools.getDataSQLCount(zhlx_btSql);
			int zhlx_nsl = DbTools.getDataSQLCount(zhlx_nslSql);
			int zhlx_tx = DbTools.getDataSQLCount(zhlx_txSql);
			int zhlx_dlf = DbTools.getDataSQLCount(zhlx_dlfSql);
			int zhlx_dmcj = DbTools.getDataSQLCount(zhlx_dmcjSql);
			List list_sw = (List)DbTools.findBySql(swrs_swSql);
			List list_sz = (List)DbTools.findBySql(swrs_szSql);
			List list_ss = (List)DbTools.findBySql(swrs_ssSql);
			List list_jjss = (List)DbTools.findBySql(jjssSql);
			int swrs_sw = 0;
			int swrs_sz = 0;
			int swrs_ss =0;
			double jjss = 0.0;
			if(list_sw.get(0)!=null)
				swrs_sw = (Integer)list_sw.get(0);
			if(list_sz.get(0)!=null)
				swrs_sz = (Integer)list_sz.get(0);
			if(list_ss.get(0)!=null)
				swrs_ss = (Integer)list_ss.get(0);
			if(list_jjss.get(0)!=null)
				jjss = (Double)list_jjss.get(0);
			ZqtjhzTable zqtjhz = new ZqtjhzTable(yue,zqzs, yfys_zr, yfys_rw, zhjb_tdx, zhjb_dx, zhjb_zx, zhjb_xxdb,
					zhjb_xxwdb, zhlx_hp, zhlx_bt, zhlx_nsl, zhlx_tx, zhlx_dlf, zhlx_dmcj, swrs_sw, swrs_sz, swrs_ss, jjss);
			resultList.add(zqtjhz);
		}
		return resultList;
	}
	
	
	public String queryCGYBTJHZ(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String pageNo = request.getParameter("page");
		int pageSize = Constants.PAGE_KEY;
		String zhCode = request.getParameter("zhCode");
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		List listDate = splitDateByStartAndEnd(startTime,endTime);
		List resultList = queryCGYBTJHZBylistDate(listDate,zhCode);
		//通过切割的日期list去遍历查询，得到各个需要的数量
		int t = listDate.size();
		PageClass page = new PageClass();
		page.setPageShow(pageSize);  //设置每页显示的行数
		page.setPageCount(t);  //设置总页数
		page.setCurrentPage(Integer.parseInt(pageNo));  //设置当前页
		int first=page.getNextIndex(); //获得开始的索引值
		int max=page.getPageShow();  //获得每次显示的值
		rows = resultList;
		total = t;
		return SUCCESS;
	}
	
	public List queryCGYBTJHZBylistDate(List listDate,String zhCode){
		List<CgybtjhzTable> resultList = new ArrayList<CgybtjhzTable>();
		for(int i = 0;i<listDate.size();i++){
			List<String> list = (List)listDate.get(i);
			String startDate = list.get(0);
			String endDate = list.get(1);
			int yue = Integer.parseInt(startDate.split("-")[1]);
			String cgybslSql = "select count(*) from YJCZ_YJCZ11 where YJCZ1104 <= '"+endDate+"' and YJCZ1104 >='"+startDate+"' and YJCZ1100 like '"+zhCode+"'";
			String bqbrrySql = "select sum(YJCZ1110) from YJCZ_YJCZ11 where YJCZ1104 <= '"+endDate+"' and YJCZ1104 >='"+startDate+"' and YJCZ1100 like '"+zhCode+"'";
			String bmswrySql ="select sum(YJCZ1120) from YJCZ_YJCZ11 where YJCZ1104 <= '"+endDate+"' and YJCZ1104 >='"+startDate+"' and YJCZ1100 like '"+zhCode+"'";
			String bmzjjjssSql = "select sum(YJCZ1125) from YJCZ_YJCZ11 where YJCZ1104 <= '"+endDate+"' and YJCZ1104 >='"+startDate+"' and YJCZ1100 like '"+zhCode+"'";
			int cgybsl = DbTools.getDataSQLCount(cgybslSql);
			List bqbrryList = (List)DbTools.findBySql(bqbrrySql);
			List bmswryList = (List)DbTools.findBySql(bmswrySql);
			List bmzjjjssList = (List)DbTools.findBySql(bmzjjjssSql);
			int bqbrry = 0;
			int bmswry = 0;
			double bmzjjjss = 0.0;
			if(bqbrryList.get(0)!=null)
				bqbrry = (Integer)bqbrryList.get(0);
			if(bmswryList.get(0)!=null)
				bmswry = (Integer)bmswryList.get(0);
			if(bmzjjjssList.get(0)!=null)
				bmzjjjss = (Double)bmzjjjssList.get(0);
			CgybtjhzTable cgybtjhz = new CgybtjhzTable(yue,cgybsl, bqbrry, bmswry, bmzjjjss);
			resultList.add(cgybtjhz);
		}
		return resultList;
	}
}